package harima.agamengine.mapmodel;

import java.awt.Dimension;
import java.io.Serializable;
import java.util.HashMap;

public class MapModel implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Dimension size;
	Dimension tileSize;
	TileModel[][] tileData;
	HashMap<String, FileReference> fileReferences;
	String mapName;
	
	public MapModel( Dimension _size, String _name)
	{
		size = validateSize(_size);
		mapName = _name;
		tileData = new TileModel[size.width][size.height];
		fileReferences = new HashMap<String, FileReference>();
		tileSize = new Dimension(32, 32);
	}
	
	public MapModel( Dimension _size, Dimension _tileSize,  String _name)
	{
		size = validateSize(_size);
		tileSize = validateSize(_tileSize);
		mapName = _name;
		tileData = new TileModel[size.width][size.height];
		fileReferences = new HashMap<String, FileReference>();
	}
	
	private Dimension validateSize(Dimension _size) {
		Dimension to = null;
		if(_size.width <0 || _size.height < 0)
		{
			to = new Dimension(1,1);
		}
		else
		{
			to = _size;
		}
		return to;
	}

	public TileModel getTile(int x, int y)
	{
		TileModel tile = null;
		if(isInMap(x, y))
		{
			tile = tileData[x][y];
		}
		return tile;
	}
	
	public void setTile(TileModel tile, int x, int y)
	{
		if(isInMap(x, y))
		{
			tileData[x][y] = tile;		
		}
	}

	public boolean isInMap(int x, int y) {
		boolean validX = (x>=0 && x<size.width);
		boolean validY = (y>=0 && y<size.height);
		return validX && validY;
	}
	
	public TileModel[][] getData(){return tileData;}
	public String getMapName(){return mapName;}
	public Dimension getSize(){return size;}
	public Dimension getTileSize() {return tileSize;}
	
	public FileReference getFileReference(String tileName) {
		return fileReferences.get(tileName);
	}

	public void addFileReference(FileReference _reference) {
		fileReferences.put(_reference.getName(), _reference);
	}



}
